$(function(){
       var url="http://www.rice.com/app/api/h5_access";
//    注册获取验证码
    $(".verification_btn").click(function(){
        var d={};
        var phone=$("#phone").val();
        var reg=/^(\+86|0086)?\s*1[34578]\d{9}$/;
	if(!reg.test(phone)){
            alert("手机号码格式不正确");
            return false;
        }
        d.method="Phonecode|send";
        d.type="register";
        d.phone=phone;
        var url="http://www.rice.com/app/api/h5_access";
        $.ajax({
            type:"post",
            url:url,
            data:d,
            dataType:"json",
            success:function(result){
                if(result.status=="0"){
                       alert(result.message);
                }else if(result.status=="1"){
                    alert(result.message);
                    var time = 180;
                    $('.verification_btn').html(time + "秒后重发").attr('disabled',true);
                        setInterval(function () {
                            time--;
                            if (time === 0) {
                                $('.verification_btn').html("发送验证码").attr('disabled',false);
                                
                            } else if (time > 0) {
                                 $('.verification_btn').html(time + "秒后重发").attr('disabled',true);
                            }

                    }, 1000);
                }else if(result.status=="2"){
                    alert(result.message);
                    location.href="login.html";
                }else{
                    alert("验证码过期");
                }
            }
            
        })
    });
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
    var you=GetQueryString("oldname");
//    console.log(you);
    if(you == "" || you == null){

    }else{
        $("#oldname").val(you).attr("disabled",true);
    }
$("#agree_tit").click(function(){
    $(this).next().show();
})
//    注册
    $("#regist_but").click(function(){
        var phone=$("#phone").val();
        var code=$("#yzm").val();
        var pwd=$("#pwd").val();
        var apwd=$("#apwd").val();
        var oldname=$("#oldname").val();
        var agree=$("#agree").val();
        var d={};
        var url="http://www.rice.com/app/api/h5_access";
        d.method="regist";
        d.phone=phone;
        d.code=code;
        d.password=pwd;
        d.referrerphone=oldname;
        if(pwd!==apwd){
            alert("两次的密码不一致");
            return false;
        }
        if(!phone){
             alert("手机号不能为空");
             return false;
        }
         if(!code){
             alert("验证码不能为空");
             return false;
        }
        if(!pwd&&!apwd){
             alert("密码不能为空");
             return false;
        }
         if(!oldname){
             alert("邀请人账号不能为空");
             return false;
        }
        if(!agree){
             alert("请先阅读用户协议");
             return false;
        }
        $.ajax({
            type:'post',
            url:url,
            data:d,
            dataType:"json",
            success:function(result){
                if(result.status=="1"){
                    console.log(1);
                    alert(result.message);
                    location.href="login.html";
                }else if(result.status=="2"){
                    alert(result.message);
                    location.href="login.html";
                }else{
                    alert(result.message);
                }
            }
        })
    });

    //找回密码获取验证
    $(".verification_btn1").click(function(){
        var d={};
        var phone=$("#phone").val();
        var reg=/^(\+86|0086)?\s*1[34578]\d{9}$/;
	if(!reg.test(phone)){
            alert("手机号码格式不正确");
            return false;
        }
        d.method="Phonecode|send";
        d.type="forget_pwd";
        d.phone=phone;
        var url="http://www.rice.com/app/api/h5_access";
        $.ajax({
            type:"post",
            url:url,
            data:d,
            dataType:"json",
            success:function(result){
                
                if(result.status=="1"){
                    alert(result.message);
                     var time = 180;
                    $('.verification_btn1').html(time + "秒后重发").attr('disabled',true);
                        setInterval(function () {
                            time--;
                            if (time === 0) {
                                $('.verification_btn1').html("发送验证码").attr('disabled',false);
                                
                            } else if (time > 0) {
                                 $('.verification_btn1').html(time + "秒后重发").attr('disabled',true);
                            }

                    }, 1000);
                }else if(result.status=="2"){
                    alert(result.message);
                    location.href="login.html";
                }else{
                    alert(result.message);
                }
            }
            
        })
    });
    //找回密码
    $("#reset_btn").click(function(){
         var phone=$("#phone").val();
        var code=$("#yzm").val();
        var pwd=$("#npwd").val();
        var apwd=$("#agpwd").val();
        var d={};
          if(!phone){
             alert("手机号不能为空");
        }
         if(!code){
             alert("验证码不能为空");
        }
         if(!pwd&&!apwd){
             alert("密码不能为空");
        }
         if(pwd!==apwd){
             alert("两次密码不一致");
        }
        d.phone=phone;
        d.newpassword=pwd;
        d.method="forgetpwd";
        $.ajax({
            type:"post",
            url:"http://www.rice.com/app/api/h5_access",
            data:d,
            dataType:"json",
            success:function(result){
                if(result.status=="1"){
                    alert(result.message);
                    location.href="collection.html";
                }
                if(result.status=="2"){
                    alert(result.message);
                    location.href="login.html";
                }
                location.reload();
            }
        })
    })
 
})